package com.sskj.api.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sskj.api.domain.*;
import com.sskj.api.service.IProductCapabilityService;
import com.sskj.api.service.IProductCategoryService;
import com.sskj.api.service.IProductService;
import com.sskj.api.vo.req.ProductCapabilityPageListReq;
import com.sskj.api.vo.req.ProductCategoryPageListReq;
import com.sskj.api.vo.req.ProductPageListReq;
import com.sskj.common.util.ApiResponseUtil;
import com.sskj.common.vo.ApiResponse;
import com.sskj.common.vo.SimplePagination;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "产品模块")
@RestController
@RequestMapping("/admin/product")
@Validated
public class ProductController {

    private IProductService productService;

    @Autowired
    private IProductCategoryService productCategoryService;

    @Autowired
    private IProductCapabilityService productCapabilityService;

    @Operation(summary = "新增产品")
    @PostMapping("add")
    public ApiResponse<Object> add(@Valid @RequestBody Product product) {
        productService.add(product);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新产品")
    @PostMapping("update")
    public ApiResponse<Object> update(@Valid @RequestBody Product product) {
        productService.update(product);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "产品分页列表")
    public ApiResponse<SimplePagination<Product>> pageList(ProductPageListReq req) {
        IPage<Product> productIPage = productService.pageList(req);

        return ApiResponseUtil.success(productIPage);
    }

    @Operation(summary = "产品详情")
    public ApiResponse<Product> detail(Long id) {
        Product detail = productService.detail(id);

        return ApiResponseUtil.success(detail);
    }

    @Operation(summary = "新增产品分类")
    @PostMapping("categoryAdd")
    public ApiResponse<Object> categoryAdd(@Valid @RequestBody ProductCategory productCategory) {
        productCategoryService.add(productCategory);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "删除产品分类")
    @PostMapping("categoryDelete")
    public ApiResponse<Object> categoryDelete(@RequestParam Long id) {
        productCategoryService.deleteById(id);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新产品分类")
    @PostMapping("categoryUpdate")
    public ApiResponse<Object> categoryUpdate(@Valid @RequestBody ProductCategory productCategory) {
        productCategoryService.update(productCategory);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "产品分类分页列表")
    @GetMapping("categoryPageList")
    public ApiResponse<SimplePagination<ProductCategory>> categoryPageList(ProductCategoryPageListReq req) {
        IPage<ProductCategory> productCategoryIPage = productCategoryService.pageList(req);

        return ApiResponseUtil.success(productCategoryIPage);
    }

    @Operation(summary = "新增产品能力")
    @PostMapping("capabilityAdd")
    public ApiResponse<Object> capabilityAdd(@Valid @RequestBody ProductCapability capability) {
        productCapabilityService.add(capability);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新产品能力")
    @PostMapping("capabilityUpdate")
    public ApiResponse<Object> capabilityUpdate(@Valid @RequestBody ProductCapability capability) {
        productCapabilityService.update(capability);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "产品能力分页列表")
    @GetMapping("capabilityPageList")
    public ApiResponse<SimplePagination<ProductCapability>> capabilityUpdatePageList(ProductCapabilityPageListReq req) {
        IPage<ProductCapability> productCapabilityIPage = productCapabilityService.pageList(req);

        return ApiResponseUtil.success(productCapabilityIPage);
    }

    @Operation(summary = "物模型绑定")
    @PostMapping("thingModelBind")
    public ApiResponse<Object> thingModelBind(@Valid @RequestBody ProductThingsModel productThingsModel) {
        productService.bindThingModel(productThingsModel);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "物模型解绑")
    @PostMapping("thingModelUnbind")
    public ApiResponse<Object> thingModelUnbind(@RequestParam(name = "bindId") Long bindId) {
        productService.unbindThingModel(bindId);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "物模型分页列表")
    @GetMapping("thingModelPageList")
    public ApiResponse<SimplePagination<ThingModelTemplate>> thingModelPageList(@RequestParam(value = "id", required = true) Long id,
                                                                                @RequestParam(value = "pageNo", defaultValue = "1") Long pageNo,
                                                                                @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize) {
        IPage<ThingModelTemplate> thingModelTemplateIPage = productService.thingModelPageList(id, pageNo, pageSize);

        return ApiResponseUtil.success(thingModelTemplateIPage);
    }
}
